
(function($){
	$.fn.jTruncate = function(options) {
	   
		var defaults = {
			length: 300,
			minTrail: 20,
			moreText: "...",
			lessText: "....",
			ellipsisText: "",
			moreAni: "",
			lessAni: ""
		};
		
		var options = $.extend(defaults, options);
	   
		return this.each(function() {
			obj = $(this);
			var body = obj.html().trim();

			if (body.length > options.length + options.minTrail) {
			    //body = body.replace('   ','')
				var splitLocation = body.indexOf(' ', options.length);
				if(splitLocation != -1) {
					// truncate tip
				    var splitLocation = body.indexOf(' ', options.length);
					var str1 = body.substring(0, splitLocation);
					var str2 = body.substring(splitLocation, body.length);
					obj.html(str1 + '<span class="truncate_ellipsis" >' + options.ellipsisText +
						'</span>' + '<span class="truncate_more">' + str2 + '</span>' +
                        '<a href="#" class="truncate_more_link">' + options.moreText + '</a>' 
                        );
					obj.find('.truncate_more').css("display", "none");
					
					// insert more link
					obj.append(
						'<div class="clearboth">' +
							
						'</div>'
					);

					// set onclick event for more/less link
                    //tam bo //duynp
//					var moreLink = $('.truncate_more_link', obj);
//					var moreContent = $('.truncate_more', obj);
//					var ellipsis = $('.truncate_ellipsis', obj);
//					moreLink.click(function() {
//						if(moreLink.text() == options.moreText) {
//							moreContent.show(options.moreAni);
//							moreLink.text(options.lessText);
//							ellipsis.css("display", "none");
//						} else {
//							moreContent.hide(options.lessAni);
//							moreLink.text(options.moreText);
//							ellipsis.css("display", "inline");
//						}
//						return false;
					//				  	});
					//--End //tam bo //duynp
				}
			} // end if
			
		});
	};
})(jQuery);